home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlb20 / lib / sbrk.c < prev    next >
C/C++ Source or Header  |  1992-05-17  |  1KB  |  68 lines

  1. /* sbrk: emulate Unix sbrk call */
  2. /* by ERS */
  3. /* jrb: added support for allocation from _heapbase when _stksize == -1 
  4.     thanks to Piet van Oostrum & Atze Dijkstra for this idea and
  5.         their diffs. */
  6.  
  7. /* WARNING: sbrk may not allocate space in continguous memory, i.e.
  8.    two calls to sbrk may not return consecutive memory. This is
  9.    unlike Unix.
  10. */
  11.  
  12. #include <osbind.h>
  13. #include <stddef.h>
  14. #include <stdlib.h>
  15. #include <errno.h>
  16.  
  17. extern void *_heapbase;
  18. extern long _stksize;
  19.  
  20. static void * HeapAlloc( sz )
  21. long sz ;
  22. {
  23.     char slush [256];
  24.     register void *sp;
  25.     
  26.     sp = (void *)slush;
  27.  
  28.  /* round up request size next octet */
  29.     sz = (sz + 7) & ~((unsigned long) 7);
  30.  
  31.     if ( sp < (void *)((char *)_heapbase + sz) )
  32.     {
  33.     return NULL;
  34.     }
  35.     sp = _heapbase;
  36.     _heapbase = (void *)((char *)_heapbase + sz);
  37.     _stksize -= (long)sz;
  38.     
  39.     return( sp );
  40. }
  41.  
  42. void *_sbrk(n)
  43.     long n;
  44. {
  45.   void *rval;
  46.  
  47.   if(_heapbase != NULL)
  48.   {
  49.       if(n) rval = HeapAlloc(n);
  50.       else  rval = _heapbase;
  51.   }
  52.   else
  53.   {
  54.       rval = (void *) Malloc(n);
  55.   }
  56.   if (rval == NULL) {
  57.       errno = ENOMEM;
  58.       rval = (void *)-1L;
  59.   }
  60.   return rval;
  61. }
  62.  
  63. void *sbrk(x)
  64. int x;
  65. {
  66.     return _sbrk((long)x);
  67. }
  68.